package com.neusoft.orcode.Utils;

import com.neusoft.hit.cryptographic.asymmetric.Rsa2;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.util.PublicKeyFactory;

import javax.crypto.Cipher;
import java.security.interfaces.RSAPrivateKey;
import java.util.Arrays;
import java.util.Map;

public class BouncyCastleRSA {

    /**
     * 调用私钥加密字符串-现用方法
     *
     * @param params
     * @return
     */
    public static String encryptedString(Map params) throws Exception {
        String organCode = (String)params.get("organCode");
        String personId = (String)params.get("personId");
        String docIdCard = (String)params.get("docIdcard");
        String adminDivisionCode = (String)params.get("adminDivisionCode");
        String medicalStatus = (String)params.get("medicalStatus");
        String appId = (String)params.get("appid");
        String cardType = (String)params.get("cardType");

        //获取公钥私钥
        String prikey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOCex9P10x4WKbXVMaClhNUbgqGRHYnY9iOvJuIRsU0t+rlLlUArhjRXYe6R9cCFf94VcfukRx2slFuKSODPkadBma3Ja6n0Js48EXU7fuYVfLuyDBP+UXapWlMR6hMBSgZSCW7OdEnfsLRUS2Wo5fMXcwwNjMX3wjTknvDi+AkEfSf8jNcZX/WZMr0189Biqm2HTMzdxWhR4oUD3N3aBwU7xA7ZWWK2cjhcTSDYNACQBNV9+OV8WbVM1LnPipKibC4ByY6i1QEISfxjeUYm1Biu9wKyVQruwH2ISkx/pGC48r6gE8EOHFN8fCoE9Lr1lA6ofcYkCScwEqHgmuNE9PAgMBAAECggEASzfyJvmxTsL+xywK4BVuYbPPBWw1STNXRTvWOoAJqWNq06B07IpsPxX/0aq+0qwQfQAidUjdCjZEtElty1QPbvTgwmPi5n2EXcnW7ZWGKBUgu2EjnXNKzLXC/bIfeFjP84jaU1KtvmazFWrfUnpuPvchGY5mwQQn50KEYLZwbXIwbz9ONWgpnnl/bQWk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FAAOCAQ8AMIIBCgKCAQEAjgnsfT9dMeFim11TGgpYTVG4KhkR2J2PYjrybiEbFNLfq5S5VAK4Y0V2HukfXAhX/eFXH7pEcdrJRbikjgz5GnQZmtyWup9CbOPBF1O37mFXy7sgwT/lF2qVpTEeoTAUoGUgluznRJ37C0VEtlqOXzF3MMDYzF98I05J7w4vgJBH0n/IzXGV/1mTK9NfPQYqpth0zM3cVoUeKFA9zd2gcFO8QO2VlitnI4XE0g2DQAkATVffjlfFm1TNS5z4qSomwuAcmOotUBCEn8Y3lGJtQYrvcCslUK7sB9iEpMf6RguPK+oBPBDhxTfHwqBPS69ZQOqH3GJAknMBKh4JrjRPTwIDAQAB";
        //私钥加密
        String url = "http://10.195.28.24:9325/rehp/#/search?";
//                    +"personId=" + pubOrPriEncryptUrl(personId, prikey)
//                    +"&docIdcard=" + pubOrPriEncryptUrl(docIdCard, prikey)
//                    +"&loginName=" + pubOrPriEncryptUrl(docIdCard, prikey)
//                    +"&adminDivisionCode=" + pubOrPriEncryptUrl(adminDivisionCode, prikey)
//                    +"&organCode=" + organCode
//                    +"&medicalStatus=" + pubOrPriEncryptUrl(medicalStatus, prikey)
//                    +"&appId=" + pubOrPriEncryptUrl(appId, prikey)
//                    +"&cardType=" + pubOrPriEncryptUrl(cardType, prikey);

        StringBuffer stringBuffer = new StringBuffer();
        String str1 = pubOrPriEncryptUrl(personId, prikey);
        String str2 = pubOrPriEncryptUrl(docIdCard, prikey);
        String str3 = pubOrPriEncryptUrl(docIdCard, prikey);
        String str4 = pubOrPriEncryptUrl(adminDivisionCode, prikey);
        String str5 = pubOrPriEncryptUrl(medicalStatus, prikey);
        String str6 = appId;
        String str7 = pubOrPriEncryptUrl(cardType, prikey);
        String str8 = pubOrPriEncryptUrl("郭泽晋", prikey);

        stringBuffer.append(url);
        if (StringUtils.isNotBlank(str1)){
            stringBuffer.append("personId=" + str1);
        }
        if (StringUtils.isNotBlank(str2)){
            stringBuffer.append("&docIdcard=" + str2);
        }
        if (StringUtils.isNotBlank(str3)){
            stringBuffer.append("&loginName=" + str3);
        }
        if (StringUtils.isNotBlank(str4)){
            stringBuffer.append("&adminDivisionCode=" + str4);
        }
        if (StringUtils.isNotBlank(organCode)){
            stringBuffer.append("&organCode="+organCode);
        }
        if (StringUtils.isNotBlank(str5)){
            stringBuffer.append("&medicalStatus=" + str5);
        }
        if (StringUtils.isNotBlank(str6)){
            stringBuffer.append("&appid=" + str6);
        }
        if (StringUtils.isNotBlank(str7)){
            stringBuffer.append("&cardType=" + str7);
        }
        if (StringUtils.isNotBlank(str8)){
            stringBuffer.append("&loginName=" + str8);
        }
        return stringBuffer.toString().replaceAll("\\+", "%2B");

    }

    /**
     * 私钥加密
     * 使用私钥加密data
     *
     * @param data
     * @param prikey
     * @return encryptData 加密数据
     */
    public static String pubOrPriEncryptUrl(String data, String prikey) throws Exception {
        if(StringUtils.isEmpty(data) || StringUtils.isEmpty(prikey)){
            return "";
        }
        //私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) Rsa2.getPrivateKeyByPkcs8(prikey);
        //私钥加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(data.getBytes("UTF-8"));
        //Base64编码
        String encryptData = Base64.encode(bytes);
        return encryptData;
    }

    /**
     * 公钥解密
     * @param data
     * @param pubkey
     * @return
     */
    public static String publicDecrypt(String data, String pubkey) throws Exception {
        if (StringUtils.isEmpty(data) || StringUtils.isEmpty(pubkey)){
            return null;
        }

        //公钥
        byte[] publicInfoByte = Base64.decode(pubkey);
        AsymmetricKeyParameter publicKey = PublicKeyFactory.createKey(publicInfoByte);
        //公钥解密
        AsymmetricBlockCipher cipher = new RSAEngine();
        byte[] encryptData = Base64.decode(data);
        //false表示解密
        cipher.init(false, publicKey);
        byte[] decryptDataArr = cipher.processBlock(encryptData, 0, encryptData.length);
        int startP = 0;
        for (byte b : decryptDataArr) {
            startP++;
            if(b == 0x00) {
                break;
            }
        }
        decryptDataArr = Arrays.copyOfRange(decryptDataArr, startP, decryptDataArr.length);
        String decryptData = new String(decryptDataArr, "UTF-8");
        return decryptData;
    }

    public static void main(String[] args) {
        byte[] publicInfoByte = Base64.decode("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjgnsfT9dMeFim11TGgpYTVG4KhkR2J2PYjrybiEbFNLfq5S5VAK4Y0V2HukfXAhX/eFXH7pEcdrJRbikjgz5GnQZmtyWup9CbOPBF1O37mFXy7sgwT/lF2qVpTEeoTAUoGUgluznRJ37C0VEtlqOXzF3MMDYzF98I05J7w4vgJBH0n/IzXGV/1mTK9NfPQYqpth0zM3cVoUeKFA9zd2gcFO8QO2VlitnI4XE0g2DQAkATVffjlfFm1TNS5z4qSomwuAcmOotUBCEn8Y3lGJtQYrvcCslUK7sB9iEpMf6RguPK+oBPBDhxTfHwqBPS69ZQOqH3GJAknMBKh4JrjRPTwIDAQAB");
        byte[] privInfoByte = Base64.decode("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOCex9P10x4WKbXVMaClhNUbgqGRHYnY9iOvJuIRsU0t+rlLlUArhjRXYe6R9cCFf94VcfukRx2slFuKSODPkadBma3Ja6n0Js48EXU7fuYVfLuyDBP+UXapWlMR6hMBSgZSCW7OdEnfsLRUS2Wo5fMXcwwNjMX3wjTknvDi+AkEfSf8jNcZX/WZMr0189Biqm2HTMzdxWhR4oUD3N3aBwU7xA7ZWWK2cjhcTSDYNACQBNV9+OV8WbVM1LnPipKibC4ByY6i1QEISfxjeUYm1Biu9wKyVQruwH2ISkx/pGC48r6gE8EOHFN8fCoE9Lr1lA6ofcYkCScwEqHgmuNE9PAgMBAAECggEASzfyJvmxTsL+xywK4BVuYbPPBWw1STNXRTvWOoAJqWNq06B07IpsPxX/0aq+0qwQfQAidUjdCjZEtElty1QPbvTgwmPi5n2EXcnW7ZWGKBUgu2EjnXNKzLXC/bIfeFjP84jaU1Ktvmaz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7dtqUJ/PLUtDdDGRNnRTndrMjsrbdNtvZFIBM0VqziG8F1QKBgAaLGIUmHg19ItNjXR97b/gF2ZVqItrk/spNM0RNqVRThGlB/Ij7R0qeDb+6q9v4QRyibweLxh1+s1ZgTkG7/MDQO/V9Ikk6bdGaLGIxOg20w0uvBsLVfwfSQw+RR1zZGYOzxFTU7jlNLLRReuz5l0A2+SLCGlaKdVZWhUObDf8PAoGAG8Rp1xs/OZZu3NFk5Ia6xfstbQgj0JWKT0gdGrx0FopPVOs/5w1qkFNLrxveTTXTp/yzr9tAji/dCVACdA1qhZiVQGwjYRz/HtZ4uaQyX+jRSrQlyKDOBPWjq1RqtGgGGw1ToZgsg/vmbXSjxUcnKXXYZZKOootffXQ2DLIStckCgYAaGQIhom7+/ktAJ8nVvop9SlmDzSt8g6Uf0AK/aRHBejbjO2WjaxK09lLWugoPKbNE/8N6vKyYdex15FwUio99/s3CthRkxGcQgS/mNxkE+ebpxcVJC5w02IXrOKl9x6OgAQ4fbSdG5nfhVZklsqkeIPrZpQ+UI+i+RUozrXnp3w==";
        String publickey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjgnsfT9dMeFim11TGgpYTVG4KhkR2J2PYjrybiEbFNLfq5S5VAK4Y0V2HukfXAhX/eFXH7pEcdrJRbikjgz5GnQZmtyWup9CbOPBF1O37mFXy7sgwT/lF2qVpTEeoTAUoGUgluznRJ37C0VEtlqOXzF3MMDYzF98I05J7w4vgJBH0n/IzXGV/1mTK9NfPQYqpth0zM3cVoUeKFA9zd2gcFO8QO2VlitnI4XE0g2DQAkATVffjlfFm1TNS5z4qSomwuAcmOotUBCEn8Y3lGJtQYrvcCslUK7sB9iEpMf6RguPK+oBPBDhxTfHwqBPS69ZQOqH3GJAknMBKh4JrjRPTwIDAQAB";
        String data = "210922195812230645";
        try {
            String encryptData = pubOrPriEncryptUrl(data, prikey);
            System.out.println(encryptData);
            System.out.println(publicDecrypt(encryptData, publickey));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
